package com.modxiageasyh.mixinglibrary.fragment

import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.fragment.app.Fragment
import androidx.navigation.Navigation
import com.modxiageasyh.mixinglibrary.MxMainActivity
import com.modxiageasyh.mixinglibrary.R
import com.owbtgad.common.base.BaseFragment
import com.owbtgad.common.utils.ToastUtils
import kotlinx.android.synthetic.main.fragment_blank.*

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
private const val TAG = "BlankFragment"
/**
 * A simple [Fragment] subclass.
 * Use the [BlankFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class BlankFragment : BaseFragment() {
    // TODO: Rename and change types of parameters
    private var param1: String? = null
    private var param2: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }


    override fun getContentLayout(): Int {
        return R.layout.fragment_blank
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val act = activity
        if (act is MxMainActivity){
            Log.d(TAG, "onViewCreated: ${act.a}")
        }
        btn_uri.setOnClickListener {
            buttonUri()
            setFocus(tttt)
            ToastUtils.showToast(mContext,"dddddddddddd")
        }
        btnSubsamplingScaleImageView.setOnClickListener {
            Navigation.findNavController(btnSubsamplingScaleImageView).navigate(R.id.action_blankFragment_to_subsamplingScaleImageViewFragment)
        }
        rl.setOnClickListener {
            setFocus(rl)
            /*Department.getStaff(0).apply {
                job()
                onSleep()
            }
            Department.getStaff(1).apply {
                job()
                onSleep()
            }*/
        }
        rl.setOnFocusChangeListener { v, hasFocus ->  rl.isSelected = hasFocus}
        tttt.setOnFocusChangeListener { v, hasFocus ->  btn_uri.isSelected = hasFocus
            tttt.isSelected = hasFocus}
        et1.setOnFocusChangeListener { _, hasFocus -> tv1.isSelected = hasFocus }
        et2.setOnFocusChangeListener { _, hasFocus -> tv2.isSelected = hasFocus }
    }

    fun setFocus(view: View?) {
        closeKeyboard()
        view?.apply {
            setFocusable(true)
            setFocusableInTouchMode(true)
            requestFocus()
//            requestFocusFromTouch()
        }
        /*view?.setFocusable(true)
        view?.setFocusableInTouchMode(true)
        view?.requestFocus()
        view?.requestFocusFromTouch()*/
    }

    private fun buttonUri() {
        val uri = "http://www.zpan.com:8080/lujing/path.htm?id=10&name=zhangsan&old=24#zuihoude"
        val mUri: Uri = Uri.parse(uri)

        // 协议
        val scheme: String? = mUri.getScheme()
        // 域名+端口号+路径+参数
        val scheme_specific_part: String = mUri.getSchemeSpecificPart()
        // 域名+端口号
        val authority: String? = mUri.getAuthority()
        // fragment
        val fragment: String? = mUri.getFragment()
        // 域名
        val host: String? = mUri.getHost()
        // 端口号
        val port: Int = mUri.getPort()
        // 路径
        val path: String? = mUri.getPath()
        // 参数
        val query: String? = mUri.getQuery()
        Log.d(TAG, "======协议===scheme ==$scheme")
        Log.d(TAG, "======域名+端口号+路径+参数==scheme_specific_part ===$scheme_specific_part")
        Log.d(TAG, "======域名+端口号===authority ==$authority")
        Log.d(TAG, "======fragment===fragment ==$fragment")
        Log.d(TAG, "======域名===host ==$host")
        Log.d(TAG, "======端口号===port ==$port")
        Log.d(TAG, "======路径===path ==$path")
        Log.d(TAG, "======参数===query ==$query")

        // 依次提取出Path的各个部分的字符串，以字符串数组的形式输出
        val pathSegments: List<String> = mUri.getPathSegments()
        for (str in pathSegments) {
            Log.d(TAG, "======路径拆分====path ==$str")
        }
        // 获得所有参数 key
        val params: Set<String> = mUri.getQueryParameterNames()
        for (param in params) {
            Log.d(TAG, "=====params=====$param")
        }

        // 根据参数的 key，取出相应的值
        val id: String? = mUri.getQueryParameter("id")
        val name: String? = mUri.getQueryParameter("name")
        val old: String? = mUri.getQueryParameter("old")
        Log.d(TAG, "======参数===id ==$id")
        Log.d(TAG, "======参数===name ==$name")
        Log.d(TAG, "======参数===old ==$old")
    }
    companion object {
        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param param1 Parameter 1.
         * @param param2 Parameter 2.
         * @return A new instance of fragment BlankFragment.
         */
        // TODO: Rename and change types and number of parameters
        @JvmStatic
        fun newInstance(param1: String, param2: String) =
            BlankFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }
}